﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class MemStorageTiming
    {
        public int Add(Chain.Model.MemStorageTiming model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MemStorageTiming(");
            builder.Append("StorageTimingMemID,StorageTimingAccount,StorageTimingTotalMoney,StorageTimingDiscountMoney,StorageTimingIsCard,StorageTimingPayCard,StorageTimingIsCash,StorageTimingPayCash,StorageTimingIsBink,StorageTimingPayBink,StorageTimingPayCoupon,StorageTimingPayType,StorageTimingPoint,StorageTimingRemark,StorageTimingShopID,StorageTimingUserID,StorageTimingCreateTime,StorageTotalTime,StorageResidueTime,StorageTimingProjectID)");
            builder.Append(" values (");
            builder.Append("@StorageTimingMemID,@StorageTimingAccount,@StorageTimingTotalMoney,@StorageTimingDiscountMoney,@StorageTimingIsCard,@StorageTimingPayCard,@StorageTimingIsCash,@StorageTimingPayCash,@StorageTimingIsBink,@StorageTimingPayBink,@StorageTimingPayCoupon,@StorageTimingPayType,@StorageTimingPoint,@StorageTimingRemark,@StorageTimingShopID,@StorageTimingUserID,@StorageTimingCreateTime,@StorageTotalTime,@StorageResidueTime,@StorageTimingProjectID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@StorageTimingMemID", SqlDbType.Int, 4), new SqlParameter("@StorageTimingAccount", SqlDbType.NVarChar, 50), new SqlParameter("@StorageTimingTotalMoney", SqlDbType.Money, 8), new SqlParameter("@StorageTimingDiscountMoney", SqlDbType.Money, 8), new SqlParameter("@StorageTimingIsCard", SqlDbType.Bit, 1), new SqlParameter("@StorageTimingPayCard", SqlDbType.Money, 8), new SqlParameter("@StorageTimingIsCash", SqlDbType.Bit, 1), new SqlParameter("@StorageTimingPayCash", SqlDbType.Money, 8), new SqlParameter("@StorageTimingIsBink", SqlDbType.Bit, 1), new SqlParameter("@StorageTimingPayBink", SqlDbType.Money, 8), new SqlParameter("@StorageTimingPayCoupon", SqlDbType.Money, 8), new SqlParameter("@StorageTimingPayType", SqlDbType.Int, 4), new SqlParameter("@StorageTimingPoint", SqlDbType.Int, 4), new SqlParameter("@StorageTimingRemark", SqlDbType.NVarChar, 200), new SqlParameter("@StorageTimingShopID", SqlDbType.Int, 4), new SqlParameter("@StorageTimingUserID", SqlDbType.Int, 4), 
                new SqlParameter("@StorageTimingCreateTime", SqlDbType.DateTime), new SqlParameter("@StorageTotalTime", SqlDbType.Int, 4), new SqlParameter("@StorageResidueTime", SqlDbType.Int, 4), new SqlParameter("@StorageTimingProjectID", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.StorageTimingMemID;
            cmdParms[1].Value = model.StorageTimingAccount;
            cmdParms[2].Value = model.StorageTimingTotalMoney;
            cmdParms[3].Value = model.StorageTimingDiscountMoney;
            cmdParms[4].Value = model.StorageTimingIsCard;
            cmdParms[5].Value = model.StorageTimingPayCard;
            cmdParms[6].Value = model.StorageTimingIsCash;
            cmdParms[7].Value = model.StorageTimingPayCash;
            cmdParms[8].Value = model.StorageTimingIsBink;
            cmdParms[9].Value = model.StorageTimingPayBink;
            cmdParms[10].Value = model.StorageTimingPayCoupon;
            cmdParms[11].Value = model.StorageTimingPayType;
            cmdParms[12].Value = model.StorageTimingPoint;
            cmdParms[13].Value = model.StorageTimingRemark;
            cmdParms[14].Value = model.StorageTimingShopID;
            cmdParms[15].Value = model.StorageTimingUserID;
            cmdParms[0x10].Value = model.StorageTimingCreateTime;
            cmdParms[0x11].Value = model.StorageTotalTime;
            cmdParms[0x12].Value = model.StorageResidueTime;
            cmdParms[0x13].Value = model.StorageTimingProjectID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int StorageTimingID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemStorageTiming ");
            builder.Append(" where StorageTimingID=@StorageTimingID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StorageTimingID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StorageTimingID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string StorageTimingIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemStorageTiming ");
            builder.Append(" where StorageTimingID in (" + StorageTimingIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int StorageTimingID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemStorageTiming");
            builder.Append(" where StorageTimingID=@StorageTimingID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StorageTimingID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StorageTimingID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool ExistsProject(int ProjectID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemStorageTiming");
            builder.Append(" where StorageTimingProjectID=@StorageTimingProjectID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StorageTimingProjectID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProjectID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetAllTimeByMem(int memid, int projectid)
        {
            return DbHelperSQL.Query(string.Format("SELECT  ISNULL(SUM(StorageResidueTime),0) AS AllTime FROM MemStorageTiming WHERE StorageTimingMemID = {0} AND StorageTimingProjectID = {1}", memid, projectid));
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM MemStorageTiming ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" * FROM MemStorageTiming ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.StorageTimingID desc");
            }
            builder.Append(")AS Row, T.*  from MemStorageTiming T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "Mem,MemStorageTiming,SysShop,SysUser,MemLevel,TimingProject";
            string[] column = new string[] { "MemStorageTiming.*,Mem.MemName,Mem.MemCard,SysShop.ShopName,SysUser.UserName,MemLevel.LevelName,TimingProject.ProjectName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "StorageTimingCreateTime", "StorageTimingID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public Chain.Model.MemStorageTiming GetModel(int StorageTimingID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 * from MemStorageTiming ");
            builder.Append(" where StorageTimingID=@StorageTimingID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StorageTimingID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StorageTimingID;
            Chain.Model.MemStorageTiming timing = new Chain.Model.MemStorageTiming();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["StorageTimingID"] != null) && (set.Tables[0].Rows[0]["StorageTimingID"].ToString() != ""))
            {
                timing.StorageTimingID = int.Parse(set.Tables[0].Rows[0]["StorageTimingID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingMemID"] != null) && (set.Tables[0].Rows[0]["StorageTimingMemID"].ToString() != ""))
            {
                timing.StorageTimingMemID = int.Parse(set.Tables[0].Rows[0]["StorageTimingMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingAccount"] != null) && (set.Tables[0].Rows[0]["StorageTimingAccount"].ToString() != ""))
            {
                timing.StorageTimingAccount = set.Tables[0].Rows[0]["StorageTimingAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["StorageTimingTotalMoney"] != null) && (set.Tables[0].Rows[0]["StorageTimingTotalMoney"].ToString() != ""))
            {
                timing.StorageTimingTotalMoney = decimal.Parse(set.Tables[0].Rows[0]["StorageTimingTotalMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingDiscountMoney"] != null) && (set.Tables[0].Rows[0]["StorageTimingDiscountMoney"].ToString() != ""))
            {
                timing.StorageTimingDiscountMoney = decimal.Parse(set.Tables[0].Rows[0]["StorageTimingDiscountMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingIsCard"] != null) && (set.Tables[0].Rows[0]["StorageTimingIsCard"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["StorageTimingIsCard"].ToString() == "1") || (set.Tables[0].Rows[0]["StorageTimingIsCard"].ToString().ToLower() == "true"))
                {
                    timing.StorageTimingIsCard = true;
                }
                else
                {
                    timing.StorageTimingIsCard = false;
                }
            }
            if ((set.Tables[0].Rows[0]["StorageTimingPayCard"] != null) && (set.Tables[0].Rows[0]["StorageTimingPayCard"].ToString() != ""))
            {
                timing.StorageTimingPayCard = decimal.Parse(set.Tables[0].Rows[0]["StorageTimingPayCard"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingIsCash"] != null) && (set.Tables[0].Rows[0]["StorageTimingIsCash"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["StorageTimingIsCash"].ToString() == "1") || (set.Tables[0].Rows[0]["StorageTimingIsCash"].ToString().ToLower() == "true"))
                {
                    timing.StorageTimingIsCash = true;
                }
                else
                {
                    timing.StorageTimingIsCash = false;
                }
            }
            if ((set.Tables[0].Rows[0]["StorageTimingPayCash"] != null) && (set.Tables[0].Rows[0]["StorageTimingPayCash"].ToString() != ""))
            {
                timing.StorageTimingPayCash = decimal.Parse(set.Tables[0].Rows[0]["StorageTimingPayCash"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingIsBink"] != null) && (set.Tables[0].Rows[0]["StorageTimingIsBink"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["StorageTimingIsBink"].ToString() == "1") || (set.Tables[0].Rows[0]["StorageTimingIsBink"].ToString().ToLower() == "true"))
                {
                    timing.StorageTimingIsBink = true;
                }
                else
                {
                    timing.StorageTimingIsBink = false;
                }
            }
            if ((set.Tables[0].Rows[0]["StorageTimingPayBink"] != null) && (set.Tables[0].Rows[0]["StorageTimingPayBink"].ToString() != ""))
            {
                timing.StorageTimingPayBink = decimal.Parse(set.Tables[0].Rows[0]["StorageTimingPayBink"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingPayCoupon"] != null) && (set.Tables[0].Rows[0]["StorageTimingPayCoupon"].ToString() != ""))
            {
                timing.StorageTimingPayCoupon = decimal.Parse(set.Tables[0].Rows[0]["StorageTimingPayCoupon"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingPayType"] != null) && (set.Tables[0].Rows[0]["StorageTimingPayType"].ToString() != ""))
            {
                timing.StorageTimingPayType = int.Parse(set.Tables[0].Rows[0]["StorageTimingPayType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingPoint"] != null) && (set.Tables[0].Rows[0]["StorageTimingPoint"].ToString() != ""))
            {
                timing.StorageTimingPoint = int.Parse(set.Tables[0].Rows[0]["StorageTimingPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingRemark"] != null) && (set.Tables[0].Rows[0]["StorageTimingRemark"].ToString() != ""))
            {
                timing.StorageTimingRemark = set.Tables[0].Rows[0]["StorageTimingRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["StorageTimingShopID"] != null) && (set.Tables[0].Rows[0]["StorageTimingShopID"].ToString() != ""))
            {
                timing.StorageTimingShopID = int.Parse(set.Tables[0].Rows[0]["StorageTimingShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingUserID"] != null) && (set.Tables[0].Rows[0]["StorageTimingUserID"].ToString() != ""))
            {
                timing.StorageTimingUserID = int.Parse(set.Tables[0].Rows[0]["StorageTimingUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingCreateTime"] != null) && (set.Tables[0].Rows[0]["StorageTimingCreateTime"].ToString() != ""))
            {
                timing.StorageTimingCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["StorageTimingCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTotalTime"] != null) && (set.Tables[0].Rows[0]["StorageTotalTime"].ToString() != ""))
            {
                timing.StorageTotalTime = int.Parse(set.Tables[0].Rows[0]["StorageTotalTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageResidueTime"] != null) && (set.Tables[0].Rows[0]["StorageResidueTime"].ToString() != ""))
            {
                timing.StorageResidueTime = int.Parse(set.Tables[0].Rows[0]["StorageResidueTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StorageTimingProjectID"] != null) && (set.Tables[0].Rows[0]["StorageTimingProjectID"].ToString() != ""))
            {
                timing.StorageTimingProjectID = int.Parse(set.Tables[0].Rows[0]["StorageTimingProjectID"].ToString());
            }
            return timing;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MemStorageTiming ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetTimeTotal(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select isnull(sum(StorageTimingTotalMoney),0) as TotalMoney,isnull(sum(StorageTimingDiscountMoney),0) as DiscountMoney,isnull(sum(StorageTotalTime),0) as TotalTime,");
            builder.Append(" isnull(sum(StorageResidueTime),0) as RemainTime,isnull(sum(StorageTimingPoint),0) as TotalPoint from MemStorageTiming,Mem,TimingProject");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where MemStorageTiming.StorageTimingMemID=Mem.MemID and MemStorageTiming.StorageTimingProjectID=TimingProject.ProjectID and " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public bool Update(Chain.Model.MemStorageTiming model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemStorageTiming set ");
            builder.Append("StorageTimingMemID=@StorageTimingMemID,");
            builder.Append("StorageTimingAccount=@StorageTimingAccount,");
            builder.Append("StorageTimingTotalMoney=@StorageTimingTotalMoney,");
            builder.Append("StorageTimingDiscountMoney=@StorageTimingDiscountMoney,");
            builder.Append("StorageTimingIsCard=@StorageTimingIsCard,");
            builder.Append("StorageTimingPayCard=@StorageTimingPayCard,");
            builder.Append("StorageTimingIsCash=@StorageTimingIsCash,");
            builder.Append("StorageTimingPayCash=@StorageTimingPayCash,");
            builder.Append("StorageTimingIsBink=@StorageTimingIsBink,");
            builder.Append("StorageTimingPayBink=@StorageTimingPayBink,");
            builder.Append("StorageTimingPayCoupon=@StorageTimingPayCoupon,");
            builder.Append("StorageTimingPayType=@StorageTimingPayType,");
            builder.Append("StorageTimingPoint=@StorageTimingPoint,");
            builder.Append("StorageTimingRemark=@StorageTimingRemark,");
            builder.Append("StorageTimingShopID=@StorageTimingShopID,");
            builder.Append("StorageTimingUserID=@StorageTimingUserID,");
            builder.Append("StorageTimingCreateTime=@StorageTimingCreateTime,");
            builder.Append("StorageTotalTime=@StorageTotalTime,");
            builder.Append("StorageResidueTime=@StorageResidueTime");
            builder.Append(" where StorageTimingID=@StorageTimingID");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@StorageTimingMemID", SqlDbType.Int, 4), new SqlParameter("@StorageTimingAccount", SqlDbType.NVarChar, 50), new SqlParameter("@StorageTimingTotalMoney", SqlDbType.Money, 8), new SqlParameter("@StorageTimingDiscountMoney", SqlDbType.Money, 8), new SqlParameter("@StorageTimingIsCard", SqlDbType.Bit, 1), new SqlParameter("@StorageTimingPayCard", SqlDbType.Money, 8), new SqlParameter("@StorageTimingIsCash", SqlDbType.Bit, 1), new SqlParameter("@StorageTimingPayCash", SqlDbType.Money, 8), new SqlParameter("@StorageTimingIsBink", SqlDbType.Bit, 1), new SqlParameter("@StorageTimingPayBink", SqlDbType.Money, 8), new SqlParameter("@StorageTimingPayCoupon", SqlDbType.Money, 8), new SqlParameter("@StorageTimingPayType", SqlDbType.Int, 4), new SqlParameter("@StorageTimingPoint", SqlDbType.Int, 4), new SqlParameter("@StorageTimingRemark", SqlDbType.NVarChar, 200), new SqlParameter("@StorageTimingShopID", SqlDbType.Int, 4), new SqlParameter("@StorageTimingUserID", SqlDbType.Int, 4), 
                new SqlParameter("@StorageTimingCreateTime", SqlDbType.DateTime), new SqlParameter("@StorageTotalTime", SqlDbType.Int, 4), new SqlParameter("@StorageResidueTime", SqlDbType.Int, 4), new SqlParameter("@StorageTimingID", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.StorageTimingMemID;
            cmdParms[1].Value = model.StorageTimingAccount;
            cmdParms[2].Value = model.StorageTimingTotalMoney;
            cmdParms[3].Value = model.StorageTimingDiscountMoney;
            cmdParms[4].Value = model.StorageTimingIsCard;
            cmdParms[5].Value = model.StorageTimingPayCard;
            cmdParms[6].Value = model.StorageTimingIsCash;
            cmdParms[7].Value = model.StorageTimingPayCash;
            cmdParms[8].Value = model.StorageTimingIsBink;
            cmdParms[9].Value = model.StorageTimingPayBink;
            cmdParms[10].Value = model.StorageTimingPayCoupon;
            cmdParms[11].Value = model.StorageTimingPayType;
            cmdParms[12].Value = model.StorageTimingPoint;
            cmdParms[13].Value = model.StorageTimingRemark;
            cmdParms[14].Value = model.StorageTimingShopID;
            cmdParms[15].Value = model.StorageTimingUserID;
            cmdParms[0x10].Value = model.StorageTimingCreateTime;
            cmdParms[0x11].Value = model.StorageTotalTime;
            cmdParms[0x12].Value = model.StorageResidueTime;
            cmdParms[0x13].Value = model.StorageTimingID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool UpdateStorageResidueTime(int StorageTimingID, int StorageResidueTime)
        {
            return (DbHelperSQL.ExecuteSql(string.Format("UPDATE MemStorageTiming SET StorageResidueTime = {0} WHERE StorageTimingID = {1}", StorageResidueTime, StorageTimingID)) > 0);
        }
    }
}

